package cn.itsource.org.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.query.EmployeeQuery;
import cn.itsource.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = {"员工接口"})
@RestController
@RequestMapping("/emp")
public class EmployeeController {
    //注入service层
    @Autowired
    private IEmployeeService service;

    /**
     * 查询所有的方法
     */
    @GetMapping
    private AjaxResult findAll(){
        //1.查询所有
        List<Employee> list = service.findAll();
        //2.封装对象返回
        return AjaxResult.createSuccess(list);
    }

    /**
     * 根据id查询的方法
     */
    @GetMapping("/findOne/{id}")
    private AjaxResult findOne(@PathVariable("id") Long id){
        return AjaxResult.createSuccess(service.findOne(id));
    }

    /**
     * 新增或者修改  没有id就是新增  没有id就是修改
     */
    @PostMapping
    @ApiOperation(value = "新增/修改员工", notes = "帅兔子")
    public AjaxResult saveOrUpdate(@ApiParam(value = "新增/修改员工", required = true) @RequestBody Employee employee){
        //调用service层
        try {
            service.saveOrUpdate(employee);
            return AjaxResult.createSuccess();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.createSuccess("系统异常,请联系最帅的管理员~~~");
        }
    }

    /**
     * 删除方法
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@ApiParam(value = "删除部门id参数", required = true) @PathVariable("id") Long id){
        //调用删除方法
        try {
            service.delete(id);
            return AjaxResult.createSuccess();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.createSuccess("系统异常,请联系最帅的管理员~~~");
        }
    }

    /**
     * 分页查询
     */
    @PostMapping("/findPage")
    private PageList<Employee> findPage(@RequestBody EmployeeQuery query){
        //调用业务层
        return service.findPage(query);
    }

    /**
     * 查询部门经理们
     */
    @PostMapping("/findManagers")
    private AjaxResult findManagers(){
        //1.查询所有
        List<Employee> list = service.findManagers();
        //2.封装对象返回
        return AjaxResult.createSuccess(list);
    }
}
